package com.cn.shine.service.impl;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import com.cn.shine.common.utils.DateUtil;
import com.cn.shine.common.utils.ExportExcelUtil;
import com.cn.shine.dao.CustomerMapper;
import com.cn.shine.pojo.Customer;
import com.cn.shine.service.CustomerService;

@Service("customerService")  
public class CustomerServiceImpl implements CustomerService {

    @Resource 
    private CustomerMapper customerMapper;
    
    @Override
    public int deleteByPrimaryKey(Integer id) {

        return customerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Customer record) {
        return customerMapper.insert(record);
    }

    @Override
    public int addCustomer(Customer customer) {
    	customer.setCreateDate(new Date());
    	if(StringUtils.isNotEmpty(customer.getBirthday())){
    	    customer.setBirthMonth(DateUtil.getMonthFromDate(customer.getBirthday()));
    	}
    	return customerMapper.insertSelective(customer);
    }

    @Override
    public Customer selectByPrimaryKey(Integer id) {
        return customerMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Customer record) {

        return customerMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Customer record) {

        return customerMapper.updateByPrimaryKey(record);
    }

	@Override
	public List<Customer> getCustomerListByPage(Customer record) {

		return customerMapper.getCustomerListByPage(record);
	}

	@Override
	public int getCountCustomerList(Customer record) {

		return customerMapper.getCountCustomerList(record);
	}

    @Override
    public void exportCustomer(HttpServletRequest request, HttpServletResponse response, Customer customer) {
        String title="会员资料";
        String[] headers = { "编号", "姓名", "联系电话", "生日", "卡上余额 " ,"住址","性别"};
        List<String[]> dataset = new ArrayList<String[]>();
        List<Customer> list = getCustomerListByPage(customer);
        String[] str = null;
        for(Customer c:list ){
            str = new String[7];
            str[0]=c.getcNum();
            str[1]=c.getcName();
            str[2]=c.getPhone();
            str[3]=c.getBirthday();
            str[4]=c.getMoney()==null?"0":String.valueOf(c.getMoney());
            str[5]=c.getAddress();
            str[6]=c.getSex();
            dataset.add(str);
        }
        try {
            OutputStream out = response.getOutputStream();
            response.setHeader(
                    "Content-disposition",
                    "attachment;filename="
                            + new String(title.getBytes("gb2312"),"ISO-8859-1") + ".xls");
            response.setContentType("application/msexcel;charset=UTF-8");// 设置类型
            response.setHeader("Pragma", "No-cache");// 设置头
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);// 设置日期头
            String rootPath = request.getSession().getServletContext()
                    .getRealPath("/");
            new ExportExcelUtil().exportExcel(rootPath, title,"","", headers, dataset, out);
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        catch(Exception e){
            e.printStackTrace();
        }
        System.out.println("excel导出成功！");
    }

    @Override
    public List<Customer> customerReport() {
        List<Customer> list = customerMapper.customerReport();
        for(Customer c:list){
            if(c.getStatus()==1&&"女".equals(c.getSex())){
                c.setcName("女性会员");
            }else if(c.getStatus()==1&&"男".equals(c.getSex())){
                c.setcName("男性会员");
            }else if(c.getStatus()==0&&"男".equals(c.getSex())){
                c.setcName("男性会员（已停用）");
            }else if(c.getStatus()==0&&"女".equals(c.getSex())){
                c.setcName("女性会员（已停用）");
            }else{
                c.setcName("其他");
            }
        }
        return list;
    }

    @Override
    public List<Customer> orderReport() {
        List<Customer> list = customerMapper.orderReport();
        for(Customer c:list){
            if("女".equals(c.getSex())){
                c.setcName("女性消费");
            }else if("男".equals(c.getSex())){
                c.setcName("男性消费");
            }else{
                c.setcName("其他消费");
            }
        }
        return list;
    }
   
}
